Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What I have done so far.
I am trying to authenticate users and admin form user table and admin table respectively.
I am using the User model as provided by laravel out of the box and created exactly same for Admin.
I have added a guard key and provider key into auth.php

Guards

'guards' => [
    'user' =>[
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

Providers

'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]
],

Routes

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('/admin', 'AdminController@index');

});

I have created a directory called AuthAdmin where Laravel's default AuthController.php and PasswordController.php files are present. (Namespace Modified accordingly)

First of all in Laravel's docs mentioned that how to specify custom guard while authenticating like this which isn't working.
enter image description here

There's another method mentioned in laravel's docs to use a guard which is not working too.

enter image description here

It would be very helpful if someone could resolve the issues and and correct me if I am wrong.

share|improve this question
    
Laravel fixed a bug in version 5.2.6. protected $guard = 'guard_name' can be used now. – imrealashu Jan 5 '16 at 15:26
    
awesome work @imrealashu - this is EXACTLY what i've been looking for :) – Zabs Jun 21 '16 at 10:09
    
Glad I could help :) – imrealashu Jun 21 '16 at 10:15
up vote 115 down vote accepted

After lots of digging and lots of questions & answers I have finally managed to work Laravel 5.2 Multi Auth with two table, So I'm writing Answer of my own Question.

How to implement Multi Auth in Larvel 5.2

As Mentioned above. Two table admin and users

Laravel 5.2 has a new artisan command.

php artisan make:auth

it will generate basic login/register route, view and controller for user table.

Make a admin table as users table for simplicity.

Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController here)

config/auth.php

//Authenticating guards
'guards' => [
    'user' =>[
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

//User Providers
'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]
],  

//Resetting Password  
'passwords' => [
    'clients' => [
        'provider' => 'client',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admin',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],  

route.php

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('/admin', 'AdminController@index');

});  

AdminAuth/AuthController.php

Add two methods and specify $redirectTo and $guard

protected $redirectTo = '/admin';
protected $guard = 'admin';
public function showLoginForm()
{
    if (view()->exists('auth.authenticate')) {
        return view('auth.authenticate');
    }

    return view('admin.auth.login');
}
public function showRegistrationForm()
{
    return view('admin.auth.register');
}  

it will help you to open another login form for admin

creating a middleware for admin

class RedirectIfNotAdmin
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
}

}

register middleware in kernel.php

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];

use this middleware in AdminController e.g.,

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    public function __construct(){
        $this->middleware('admin');
   }
public function index(){
        return view('admin.dashboard');
    }
}

That's all needed to make it working and also to get json of authenticated admin use
Auth::guard('admin')->user()

Edit - 1
We can access authenticated user directly using
Auth::user() but if you have two authentication table then you have to use

Auth::guard('guard_name')->user()  

for logout

Auth::guard('guard_name')->user()->logout()

for authenticated user json

Auth::guard('guard_name')->user()  

Edit 2

Now you can download Laravel 5.2 Multiauth implemented Project http://imrealashu.in/code/laravel/multi-auth-with-laravel-5-2-2/

share|improve this answer
    
thanks a lot man, this was what i was searching for, it worked with a litle bit of modification. thanks a lot +1 for Q and +1 for Ans, wish i could +1 more. thanks a lot.. – rummykhan Feb 2 '16 at 8:22
    
@imrealashu, As I have created project as per your answer. But it generates error of multiple classes with same name. So what about that? If I changes class name for admin auth, then where I need to do changes? – Akshay Vaghasiya Feb 22 '16 at 6:43
1  
can you please also explain how to "Reset Password" for admin guard. – Shoaib Rehan Mar 4 '16 at 6:37
1  
Thanks for the excellent explanation was very useful to me everything. To use the middleware guest, change the RedirectIfAuthenticated.php file the following line: Original: if (Auth :: guard ($ guard) -> check ()) { return redirect ('/'); } After the change: if (Auth :: guard ('yourcustomguard') -> check () || Auth :: check ()) { return redirect ('/'); } – Cristian Meza Mar 29 '16 at 3:06
1  
@Jeffz its just because of bad documentation and no working examples on multi-auth.. we expect good documentation and yeah the new cool features on this 5.3 update. – imrealashu Aug 1 '16 at 22:31

In case this helps anyone, and this may just be due to my lack of understanding of middleware, here's what I had to do to get this working (in addition to the steps taken by @imrealashu)...

In route.php:

Route::get('/admin', [
  'middleware' => 'admin',
  'uses' => 'AdminController@index'
]);

This is in the web middleware group. Before this I tried putting it in a separate admin middleware group and even in an auth:admin group but this didn't work, it only worked for me when I specified the middleware as admin on the route itself. I have no idea why this is but I hope it saves others from pulling their hair out like I did.

share|improve this answer
    
I downloaded your multi auth zip file replaced with ezisting project files then when I migrate my DB this error show..[Symfony\Component\Console\Exception\RuntimeException] Not enough arguments (missing: "name"). – G Naga Subrahmanyam Oct 31 '16 at 11:14
    
Actually for me admin is logging in but not redirected to admin. After doing this it works can you please tell why's so? I got to register other routes as is it possible with Route::group(['middleware' => ['admin']], function () { //Admin Routes... }); cause it's not working for me – TheAkashRajput Nov 11 '16 at 5:50

protected by Community Mar 23 '16 at 22:18

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.